package demo02.MyReflect.myreflect1;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ReflectDemo1 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取系统类加载器，加载prop1.properties文件
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("demo02/MyReflect/prop1.properties");

        //创建properties 集合
        Properties prop=new Properties();

        prop.load(is);
        is.close();

        //--------下面的代码就是利用反射创建对象并调用方法---------------------
        //获取字节码文件对象
        Class className = Class.forName(prop.getProperty("className"));
        //获取构造器对象
        Constructor constructor = className.getConstructor();
        //利用构造器对象创建一个对象
        Object o = constructor.newInstance();
        //获取方法对象
        Method methodName = className.getMethod(prop.getProperty("methodName"));
        //运行方法
        methodName.invoke(o);
    }
}
